package com.redroid.findme;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class DetailnEdit extends Activity{

	TextView dtlsn, dtluser, dtlpass;
	Button edit, back, delete;
	Contact mycontact;
	DbHandler db;
	@Override
	protected void onCreate(Bundle dtlnedit) {
		// TODO Auto-generated method stub
		super.onCreate(dtlnedit);
		setContentView(R.layout.detailnedit);

		dtlsn = (TextView)findViewById(R.id.detailneditsn);
		dtluser = (TextView)findViewById(R.id.detailneditusername);
		dtlpass = (TextView)findViewById(R.id.detailneditpassword);
		edit = (Button)findViewById(R.id.editdetailbutton);
		back = (Button)findViewById(R.id.backdetailbutton);
		delete = (Button)findViewById(R.id.deletedetailbutton);
		
		Bundle rcvbundle = getIntent().getExtras();
		int contactid = rcvbundle.getInt("contid");
		
		if (contactid != 0) {
			db = new DbHandler(this);
			db.getReadableDatabase();
			mycontact = db.getContact(contactid);
			dtlsn.setText(mycontact.getSN());
			dtluser.setText(mycontact.getUsername());
			dtlpass.setText(mycontact.getPassword());
			db.close();
		}else{
			Log.d("Objct", "O id não foi repassado");
		}
		
		//TODO Implementar algoritmo de edição de detalhes
		edit.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				DetailPopup(mycontact.getSN());
			}
		});
		
		back.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onPause();
			}
		});
		
		delete.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				deletepopup();
			}
		});
	}
	
	/*
	    * Popup para input de detalhes
	    * @param socialnetwork string
	    */
	   public void DetailPopup(String socialnetwork){
		   LayoutInflater linf = LayoutInflater.from(this);
		   View popup = linf.inflate(R.layout.accinputdialogxml, null);

		   	AlertDialog.Builder adbuilder = new AlertDialog.Builder(this);		   
		   	adbuilder.setView(popup);

		   	final EditText usernameinput = (EditText)popup.findViewById(
		   			R.id.usernameinput);
		   	final EditText passwordinput = (EditText)popup.findViewById(
		   			R.id.passwordinput);
		   	final TextView snlabelpopup = (TextView)popup.findViewById(
		   			R.id.snlabelpopup);
		   	snlabelpopup.setText(socialnetwork, null);
		   	
		   	adbuilder.setCancelable(false).setPositiveButton("OK", 
		   			new DialogInterface.OnClickListener() {
		   			public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub

		   			mycontact.setUsername(((usernameinput.getText().
		   					toString())));	
		   			mycontact.setPassword(((passwordinput.getText().
		   					toString())));
		   			db.updateContact(mycontact);
		   			db.close();
		   			dtluser.setText(mycontact.getUsername());
					dtlpass.setText(mycontact.getPassword());
				}
			}).setNegativeButton("Cancel", new DialogInterface
					.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
				}
			});

		   	AlertDialog alrtdialog = adbuilder.create();
		   	alrtdialog.show();
	   }
	
	   public void deletepopup(){
		   final AlertDialog deletewrn = new AlertDialog.Builder(this).create();
		   deletewrn.setTitle("Delete?");
		   deletewrn.setMessage("Performing this action you will lose " + 
				   mycontact.getUsername() + " details.");
		   deletewrn.setButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				db.deleteContact(mycontact);
				onPause();
			}
		});
		   deletewrn.setButton2("Cancel", new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialog, int which) {
				deletewrn.dismiss();
			}
		});
		   deletewrn.show();
	   }
	   
	   
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		db.close();
		finish();
	}
	
}
